<template>

  <el-row>
    <!-- <el-col :span="4">
      <div class="box">
        <el-scrollbar>
          <basic-container>
            <avue-tree :option="treeOption" :data="treeData"  @node-click="nodeClick" :defaultExpandAll="true" />
          </basic-container>
        </el-scrollbar>
      </div>
    </el-col> -->
    <!-- <el-col :span="20"> -->
    <basic-container>
      <avue-crud :option="option"
                 :table-loading="loading"
                 :data="data"
                 :page="page"
                 :permission="permissionList"
                 :before-open="beforeOpen"
                 v-model="form"
                 ref="crud"
                 @row-update="rowUpdate"
                 @row-save="rowSave"
                 @row-del="rowDel"
                 @search-change="searchChange"
                 @search-reset="searchReset"
                 @selection-change="selectionChange"
                 @current-change="currentChange"
                 @size-change="sizeChange"
                 @on-load="onLoad" v-if="showMain">
        <template slot="menuLeft">
          <el-button type="danger"
                     size="small"
                     icon="el-icon-delete"
                     plain
                     v-if="permission.jf_delete"
                     @click="handleDelete">删 除
          </el-button>
        </template>
        <template slot-scope="{type,size,row,index}" slot="menuBtn">
          <el-dropdown-item divided @click.native="endHomeVisit(row)" v-show="row.state !== '4'">结束家访
          </el-dropdown-item>
          <el-dropdown-item divided v-show="row.state === '4'" @click.native="feedbackHomeVisit(row)">家访反馈
          </el-dropdown-item>
          <el-dropdown-item divided @click.native="pushMessage(row)" v-show="row.jfIsMessage === false">推送消息
          </el-dropdown-item>
        </template>
        <!-- <template slot-scope="{type,size,row,index}"  slot="menuBtn">
          <el-dropdown-item divided @click.native="showTView(row,index)">查看家访对象</el-dropdown-item>
          <el-dropdown-item divided @click.native="showRView(row,index)">查看家访记录</el-dropdown-item>
        </template> -->
        <!--         <template slot-scope="{type,size}" slot="menu">-->
        <!--          <el-button icon="el-icon-start" :size="size" :type="type">开始家访</el-button>-->
        <!--        </template>-->
      </avue-crud>
      <!--      <Jftarget v-bind:jfId="jfId" :showMainView="showMainView" v-if="(!showMain)&&showT" ref="eps1"></Jftarget>-->
      <!--      <Jfrecords v-bind:jfId="jfId" :showMainView="showMainView" v-if="(!showMain)&&showR" ref="eps2"></Jfrecords>-->

      <el-dialog title="家访反馈"
                 append-to-body
                 :visible.sync="excelBox"
                 width="555px">
        <avue-form :option="excelOption" v-model="excelForm" :upload-after="uploadAfter">
        </avue-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitrep">确 定</el-button>
          <el-button @click="excelBox = false">取 消</el-button>
        </span>
      </el-dialog>
    </basic-container>
    <!-- </el-col> -->
  </el-row>

</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove,
  finishVisiting,
  getFeedbackRecordInfo, editFeedbackRecord, getTeacherByRelateId, pushVisitingMessage
} from "@/api/service/jf";
import {mapGetters} from "vuex";

import Jftarget from "@/views/service/jftarget";

import Jfrecords from "@/views/service/jfrecords";
import {contrastTime} from "@/util/validate";
// import {getStore} from "@/util/store";
// import {getBjTree} from "@/api/service/org";


export default {
  components: {
    Jftarget,
    Jfrecords
  }
  ,
  data() {
    return {
      showSchool: false,
      treeBj: '0',
      jfId: "",
      showMain: true,
      showR: false,
      showT: false,
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      excelBox: false,
      excelForm: {},
      selectData: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "反馈内容",
            prop: "recordContent",
            // span: 24,
            // disabled: true,
            type: "textarea",
            span: 24,
            minRows: 6,
            rules: [{
              required: true,
              message: "请输入反馈内容",
              trigger: "blur"
            }]
          },
        ]
      },
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        menuType: 'menu',
        menuBtnTitle: '更多操作',
        column: [
          {
            label: "主键",
            prop: "id",
            rules: [{
              required: true,
              message: "请输入主键",
              trigger: "blur"
            }],
            hide: true,
            display: false
          },
          {
            label: "学校",
            prop: "schoolName",
            rules: [{
              required: false,
              message: "请输入学校名称",
              trigger: "blur"
            }],
            // type: "select",
            // dicUrl: "/api/PcPublicService/GetSelectSchoolList",
            // props: {
            //   label: "xxName",
            //   value: "id"
            // },
            editDisabled: true,
            addDisabled: true
          },
          {
            label: "教师",
            prop: "jsName",
            search: false,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入教师姓名",
              trigger: "blur"
            }],
            // type: "select",
            // dicUrl: "/api/school-service/js/select",
            // props: {
            //   label: "jsName",
            //   value: "id"
            // },
            // dataType: "number",
            editDisabled: true,
            addDisabled: true,
          },
          {
            label: "年级",
            prop: "gradeName",
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择年级",
              trigger: "blur"
            }],
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            editDisabled: true,
          },
          {
            label: "年级",
            prop: "gradeId",
            search: true,
            overHidden: true,
            hide: true,
            rules: [{
              required: true,
              message: "请选择年级",
              trigger: "blur"
            }],
            type: "select",
            cascader: ['classId'],
            dicUrl: "/api/ManageHomeVisiting/GetTeacherGradeSelect",
            props: {
              label: "name",
              value: "id"
            },
            // dataType: "number",
            editDisabled: true,
            // addDisabled: true,
          },
          {
            label: "学生班级",
            prop: "className",
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择学生班级",
              trigger: "blur"
            }],
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            editDisabled: true,
          },
          {
            label: "学生班级",
            prop: "classId",
            rules: [{
              required: true,
              message: "请选择学生班级",
              trigger: "blur"
            }],
            hide: true,
            type: "select",
            cascader: ['studentId', 'coursetId'],
            dicUrl: "/api/ManageHomeVisiting/GetTeacherClassSelect?njId={{key}}",
            props: {
              label: "name",
              value: "id"
            },
            // dicData: [],
            // type: "tree",
            editDisabled: true,
            slot: true,
            search: true,
          },
          {
            label: "课程",
            prop: "coursetTitle",
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择课程",
              trigger: "blur"
            }],
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            editDisabled: true,
          },
          {
            label: "课程",
            prop: "coursetId",
            rules: [{
              required: true,
              message: "请选择课程",
              trigger: "blur"
            }],
            hide: true,
            type: "select",
            // dicData: [],
            dicUrl: "/api/ManageHomeVisiting/GetTeacherCourseSelect?bjId={{key}}",
            props: {
              label: "name",
              value: "id"
            },
            // hide: true,
            editDisabled: true
          },
          {
            label: "学生",
            prop: "studentName",
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择学生",
              trigger: "blur"
            }],
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            editDisabled: true,
          },
          {
            label: "学生",
            prop: "studentId",
            rules: [{
              required: true,
              message: "请输入学生名称",
              trigger: "blur"
            }],
            hide: true,
            slot: true,
            search: true,
            checkStrictly: true,
            editDisabled: true,
            type: "select",
            dicUrl: "/api/ManageHomeVisiting/GetClassStudentSelect?bjId={{key}}",
            props: {
              label: "name",
              value: "id"
            },
            // type: "tree",
            // dicData: [],
            // props: {
            //   label: "studentName",
            //   value: "studentId"
            // },
            // nodeClick: (data, node) => {
            //   console.log("学生,", node)
            //   // this.form.schoolId = node.data.schoolId;
            //   // this.form.schoolName = node.data.schoolName;
            //   // this.form.classId = node.data.classId;
            //   // // this.form.className = node.data.className;
            //   // this.form.teacherId = node.data.teacherId;
            //   // this.form.teacherName = node.data.teacherName;
            //
            // },

          },


          // {
          //   label: "班主任",
          //   prop: "isBzr",
          //   rules: [{
          //     required: true,
          //     message: "请输入是否班主任",
          //     trigger: "blur"
          //   }],
          //   type: "select",
          //   dicData: [
          //
          //     {
          //       label: "否",
          //       value: 0
          //     },
          //     {
          //       label: "是",
          //       value: 1
          //     },
          //
          //   ],
          //   dataType:'number',
          //   editDisabled:true,
          //   addDisabled:true
          // },
          {
            label: "类型",
            prop: "type",
            search: false,
            rules: [{
              required: true,
              message: "请输入类型",
              trigger: "blur"
            }],
            type: "select",
            dicData: [

              {
                label: "在线视频",
                value: 1
              },
              {
                label: "在线留言",
                value: 2
              }
            ],
            dataType: 'number',
            editDisabled: true
          },
          {
            label: "主题",
            prop: "title",
            overHidden: true,
            span: 12,
            rules: [{
              required: true,
              message: "请输入主题",
              trigger: "blur"
            }],
            search: true,
            editDisabled: true,
            maxlength: 100,
            showWordLimit: true
          },


          {
            label: "家访内容",
            prop: "desc",
            type: "textarea",
            span: 24,
            rules: [{
              required: true,
              message: "请输入家访内容",
              trigger: "blur"
            }],
            hide: true,
            maxlength: 500,
            editDisabled: true
          },
          {
            label: "开始时间",
            prop: "startTime",
            type: "datetime",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择开始时间",
              trigger: "blur"
            }],
            editDisabled: true
          },
          {
            label: "结束时间",
            prop: "endTime",
            type: "datetime",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择结束时间",
              trigger: "blur"
            }],
            editDisabled: true
          },

          {
            label: "家访状态",
            prop: "state",
            search: false,
            rules: [{
              required: true,
              message: "请输入家访状态",
              trigger: "blur"
            }],
            type: "select",
            dicData: [

              {
                label: "未开始",
                value: 0
              },
              {
                label: "已拒绝",
                value: 1
              },
              {
                label: "已预约",
                value: 2
              },
              {
                label: "进行中",
                value: 3
              },
              {
                label: "已完成",
                value: 4
              }
            ],
            value: 0,
            dataType: 'number',
            addDisabled: true,
            editDisabled: true
          },
          // {
          //   label: "家访总结",
          //   prop: "summary",
          //   type: "textarea",
          //   span: 24,
          //   minRows: 6,
          //   rules: [{
          //     required: false,
          //     message: "请输入家访总结",
          //     trigger: "blur"
          //   }],
          //   hide: true,
          //   maxlength: 500,
          //   showWordLimit: true,
          //   addDisplay: false,
          //   editDisplay: false,
          //   viewDisplay: true,
          // },
          {
            label: "评价标签",
            prop: "evaluate_tags",
            span: 24,
            rules: [{
              required: false,
              message: "请输入评价标签",
              trigger: "blur"
            }],
            hide: true,
            addDisabled: true,
            editDisabled: true,
            type: 'select',
            multiple: true,
            button: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: true,
            dicData: [{
              label: '内容有趣',
              value: '内容有趣'
            }, {
              label: '有针对性',
              value: '有针对性'
            }, {
              label: '有新意',
              value: '有新意'
            }, {
              label: '态度亲和',
              value: '态度亲和'
            }, {
              label: '有学习性',
              value: '有学习性'
            }, {
              label: '建议中肯',
              value: '建议中肯'
            }]
          },
          {
            label: "老师态度",
            prop: "evaluate1",
            span: 8,
            rules: [{
              required: false,
              message: "请输入老师态度",
              trigger: "blur"
            }],
            hide: true,
            type: "rate",
            dataType: "number",
            // addDisabled: true,
            // editDisabled: true,
            showScore: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: true,
          },
          {
            label: "问题解决",
            prop: "evaluate2",
            span: 8,
            rules: [{
              required: false,
              message: "请输入问题解决",
              trigger: "blur"
            }],
            hide: true,
            type: "rate",
            dataType: "number",
            addDisabled: true,
            editDisabled: true,
            showScore: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: true,
          },
          {
            label: "总体评分",
            span: 8,
            prop: "evaluate3",
            rules: [{
              required: false,
              message: "请输入总体评分",
              trigger: "blur"
            }],
            hide: true,
            type: "rate",
            dataType: "number",
            addDisabled: true,
            editDisabled: true,
            showScore: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: true,
          },
        ]
      },
      data: []
    };
  },
  mounted() {
    this.getSchoolTeacher()
    // var userinfo = getStore({
    //   name: "userInfo",
    // });
    // getBjTree({jsId:userinfo.relateId}).then(res => {
    //   this.treeData = res.data.data;
    // });
  },
  computed: {
    ...mapGetters(["permission", "JSuserInfo"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.jf_add, false),
        viewBtn: this.vaildData(this.permission.jf_view, false),
        delBtn: this.vaildData(this.permission.jf_delete, false),
        editBtn: this.vaildData(this.permission.jf_edit, false)
      };
    },
    js_Id() {
      return this.JSuserInfo.js_Id
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    // nodeClick(data,e,t){
    //   if(data.type!=3) return;
    //   console.log(e.parent.label)
    //   console.log(e.parent.parent.label)
    //   this.treeBj = data.id;
    //   this.onLoad(this.page);
    // },
    // 结束家访
    endHomeVisit(row) {
      this.$confirm("是否结束家访?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let data = {
            id: row.id,
          }
          return finishVisiting(data);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    // 家访反馈
    feedbackHomeVisit(row) {
      console.log(row)
      getFeedbackRecordInfo(row.id).then((res) => {
        this.selectData = row
        this.excelBox = true
        this.excelForm.recordContent = res.data.data === null ? '' : res.data.data.recordContent
      }, error => {
        window.console.log(error);
      });
    },
    // 反馈弹窗确定按钮
    submitrep() {
      if (this.excelForm.recordContent === '' || this.excelForm.recordContent === undefined || this.excelForm.recordContent === null) {
        this.$message({
          type: "warning",
          message: "反馈内容不可为空!"
        });
        return 0
      }
      let params = {
        id: this.selectData.id,
        RecordContent: this.excelForm.recordContent
      }
      editFeedbackRecord(params).then(() => {
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        this.excelBox = false
      }, error => {
        window.console.log(error);
      });

    },
    // 推送消息
    pushMessage(row) {
      this.$confirm("推送消息后内容不可更改，是否推送消息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let data = {
            id: row.id,
          }
          return pushVisitingMessage(data);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });

    },


    showTView(row) {
      this.jfId = row.id;
      this.showMain = false;
      this.showT = true;
      this.showR = false;
    },
    showRView(row) {
      this.tzId = row.id;
      this.showMain = false;
      this.showT = false;
      this.showR = true;
    },
    showMainView() {
      this.showMain = true;
      this.showT = false;
      this.showR = false;
    },
    rowSave(row, done, loading) {
      console.log(row)
      if (!contrastTime(row.startTime,row.endTime)) {
        this.$message({
          type: "warning",
          message: "开始时间不可大于结束时间!"
        });
        loading();
        return 0
      }
      row.id = '0'
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      if (!contrastTime(row.startTime,row.endTime)) {
        this.$message({
          type: "warning",
          message: "开始时间不可大于结束时间!"
        });
        loading();
        return 0
      }
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let data = {
            ids: row.id,
            // AppUserId:this.js_Id
          }
          return remove(data);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let data = {
            ids: this.ids,
            // AppUserId:this.js_Id
          }
          return remove(data);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      console.log(this.JSuserInfo)
      // this.form.teacherName = this.JSuserInfo.name
      // this.form.TeacherId = this.js_Id
      if (["edit"].includes(type)) {
        getDetail(this.form.id).then(res => {
          if(res.data.data.jfIsMessage){
            this.$refs.crud.option.column.filter(item => {
              // if (item.prop === "code") {
              //   item.value = row.code;
              //   item.addDisabled = true;
              // }
              item.editDisabled = true;
            });

          }else {
            this.$refs.crud.option.column.filter(item => {
              item.editDisabled = false;
              if (item.prop === "state") {
                item.addDisabled = true;
              }
            });
          }
        });
      }
      if (["add"].includes(type)) {
        this.getSchoolTeacher()
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      // params.AppUserId = this.js_Id;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
      // StudentList().then(res => {
      //   const column = this.findObject(this.option.column, "studentId");
      //   column.dicData = res.data.data;
      // });
      // CourseList().then(res => {
      //   const column = this.findObject(this.option.column, "coursetTitle");
      //   column.dicData = res.data.data;
      // })
      // getBjTree(this.form.tenantId).then(res => {
      //   const column = this.findObject(this.option.column, "classId");
      //   column.dicData = res.data.data;
      // });
      // const school = this.findObject(this.option.column, "schoolId");
      // school.hide = true;

    },
    uploadAfter(res, done, loading, column) {
      window.console.log(column);
      done();
      this.excelBox = false;
    },
    getSchoolTeacher() {
      getTeacherByRelateId().then(res => {
        this.form.schoolName = res.data.data.schoolName
        this.form.schoolId = res.data.data.schoolId
        this.form.jsName = res.data.data.jsName
        this.form.jsId = res.data.data.jsId
        console.log(res)
      });
    },
  }
};
</script>

<style>
</style>
